﻿using System;
using System.Windows.Input;
using Present.Commands.Async.Actions;

namespace Present.Commands.Decorators
{
    /// <summary>
    /// Listens to an observable and fires canExecutedChanged when the observable returns an item
    /// </summary>
    public class CanExecuteChangedNotifierDecorator<TInnerCommand, TObservable> : CommandDecorator<TInnerCommand> where TInnerCommand : ICommand
    {
        private IDisposable subscription;

        public CanExecuteChangedNotifierDecorator(IObservable<TObservable> notifier, TInnerCommand innerCommand)
            : base(innerCommand)
        {
            subscription = notifier.Subscribe(e => RaiseCanExecuteChanged());
        }

    }

    public static class CanExecuteChangedNotifier
    {
        public static ICommand CanExecuteChangedWhen<TInnerCommand, TObservable>(this TInnerCommand innerCommand, IObservable<TObservable> notifier) where TInnerCommand : ICommand
        {
            return new CanExecuteChangedNotifierDecorator<TInnerCommand, TObservable>(notifier, innerCommand);
        }
    }
}
